package com.hznu.jspFinal.service;

import com.hznu.jspFinal.dao.*;
import com.hznu.jspFinal.entity.*;
import com.hznu.jspFinal.myBeans.Exercise;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * author : wp
 * time: 21:12 2018/06/07
 */
@Service
public class ExerciseServiceImpl implements ExerciseService{


    @Autowired
    private ExerciseInfoMapper exerciseInfoMapper;

    @Autowired
    private TrueFalseMapper trueFalseMapper;

    @Autowired
    private ChoiceMapper choiceMapper;

    @Autowired
    private ShortAnswerMapper shortAnswerMapper;

    @Autowired
    private CompletionMapper completionMapper;


    @Override
    public void addOne(Exercise exercise)
    {
        exerciseInfoMapper.insertSelective(exercise.getExerciseInfo());
        int exerciseInfoId = exercise.getExerciseInfo().getId();
        switch (exercise.getExerciseInfo().getType())
       {
           case 1:exercise.getChoice().setExerinfoid(exerciseInfoId);
               choiceMapper.insertSelective(exercise.getChoice());
               break;
           case 2:exercise.getTrueFalse().setExerinfoid(exerciseInfoId);
               trueFalseMapper.insertSelective(exercise.getTrueFalse());
               break;
           case 3:exercise.getCompletion().setExerinfoid(exerciseInfoId);
               completionMapper.insertSelective(exercise.getCompletion());
               break;
           case 4:exercise.getShortAnswer().setExerinfoid(exerciseInfoId);
               shortAnswerMapper.insertSelective(exercise.getShortAnswer());
               break;
       }
    }
    public void update(Exercise exercise)
    {
        exerciseInfoMapper.updateByPrimaryKeySelective(exercise.getExerciseInfo());

        switch (exercise.getExerciseInfo().getType())
        {
            case 1: choiceMapper.updateByPrimaryKeySelective(exercise.getChoice());
                break;
            case 2: trueFalseMapper.updateByPrimaryKeySelective(exercise.getTrueFalse());
                break;
            case 3:completionMapper.updateByPrimaryKeySelective(exercise.getCompletion());
                break;
            case 4:shortAnswerMapper.updateByPrimaryKeySelective(exercise.getShortAnswer());
                break;
        }
    }
    public void delete(Integer id){
        ExerciseInfo exerciseInfo = exerciseInfoMapper.selectByPrimaryKey(id);

        //删除题型中的记录
        switch (exerciseInfo.getType())
        {
            case 1:Choice choice = choiceMapper.selectByExerinfoid(exerciseInfo.getId());
                choiceMapper.deleteByPrimaryKey(choice.getId());
                break;
            case 2:TrueFalse trueFalse = trueFalseMapper.selectByExerinfoid(exerciseInfo.getId());
                trueFalseMapper.deleteByPrimaryKey(trueFalse.getId());
                break;
            case 3:Completion completion = completionMapper.selectByExerinfoid(exerciseInfo.getId());
                completionMapper.deleteByPrimaryKey(completion.getId());
                break;
            case 4:ShortAnswer shortAnswer = shortAnswerMapper.selectByExerinfoid(exerciseInfo.getId());
                shortAnswerMapper.deleteByPrimaryKey(shortAnswer.getId());
                break;
        }
        //删除题目情况表中的记录
        exerciseInfoMapper.deleteByPrimaryKey(id);
    }

    public Exercise findByExerinfoid(Integer id)
    {
        Exercise exercise = new Exercise();
        ExerciseInfo exerciseInfo = exerciseInfoMapper.selectByPrimaryKey(id);

        exercise.setExerciseInfo(exerciseInfo);

        switch (exerciseInfo.getType())
        {
            case 1:exercise.setChoice(choiceMapper.selectByExerinfoid(exerciseInfo.getId()));
                break;
            case 2:exercise.setTrueFalse(trueFalseMapper.selectByExerinfoid(exerciseInfo.getId()));
                break;
            case 3:exercise.setCompletion(completionMapper.selectByExerinfoid(exerciseInfo.getId()));
                break;
            case 4:exercise.setShortAnswer(shortAnswerMapper.selectByExerinfoid(exerciseInfo.getId()));
                break;
        }
        return exercise;
    }


    @Override
    public List<Exercise> findByKpointId(Integer id) {

        //获取知识点对应的题目情况记录集
        List<ExerciseInfo> exerciseInfos = exerciseInfoMapper.selectByKpointId(id);

        //题目情况表和题目一一对应包装成Exercise类
        return getExercisesByInfos(exerciseInfos);
    }

    @Override
    public List<Exercise> findByCourseId(Integer id) {

        //获取知识点对应的题目情况记录集
        List<ExerciseInfo> exerciseInfos = exerciseInfoMapper.selectByCourseId(id);

        //题目情况表和题目一一对应包装成Exercise类
        return getExercisesByInfos(exerciseInfos);
    }
    @Override
    public List<Exercise> getExercisesByInfos(List<ExerciseInfo> exerciseInfos)
    {
        List<Exercise> exercises = new ArrayList<>();

        for(int i=0;i<exerciseInfos.size();i++){
            Exercise exercise = new Exercise();
            exercise.setExerciseInfo(exerciseInfos.get(i));
            switch (exercise.getExerciseInfo().getType())
            {
                case 1:
                    exercise.setChoice(choiceMapper.selectByExerinfoid(exercise.getExerciseInfo().getId()));
                    break;
                case 2:
                    exercise.setTrueFalse(trueFalseMapper.selectByExerinfoid(exercise.getExerciseInfo().getId()));
                    break;
                case 3:
                    exercise.setCompletion(completionMapper.selectByExerinfoid(exercise.getExerciseInfo().getId()));
                    break;
                case 4:
                    exercise.setShortAnswer(shortAnswerMapper.selectByExerinfoid(exercise.getExerciseInfo().getId()));
                    break;
            }
            exercises.add(exercise);
        }

        return exercises;
    }

    private Boolean isExerciseMatch(ExerciseInfo exerciseInfo,Integer courseid,Integer kpointid)
    {
        return ((courseid == null || exerciseInfo.getCourseid() == courseid) &&
                (kpointid == null || exerciseInfo.getKpointid() == kpointid));
    }

    @Override
    public List<Exercise> findByKeyword(String keyword,Integer courseid,Integer kpointid) {

        List<ExerciseInfo> exerciseInfos = new ArrayList<>();

        List<TrueFalse> trueFalses = trueFalseMapper.selectByKeyword(keyword);

        List<Choice> choices = choiceMapper.selectByKeyword(keyword);

        List<Completion> completions = completionMapper.selectByKeyword(keyword);

        List<ShortAnswer> shortAnswers = shortAnswerMapper.selectByKeyword(keyword);

        //将4种题型对应exerciseinfo
        int i=0;
        ExerciseInfo exerciseInfo;
        if(trueFalses != null){
            for(i=0;i<trueFalses.size();i++){
                exerciseInfo = exerciseInfoMapper.selectByPrimaryKey(trueFalses.get(i).getExerinfoid());
                if(isExerciseMatch(exerciseInfo,courseid,kpointid)){
                        exerciseInfos.add(exerciseInfo);
                }
            }
        }

        if(choices != null){
            for(i=0;i<choices.size();i++){
                exerciseInfo = exerciseInfoMapper.selectByPrimaryKey(choices.get(i).getExerinfoid());
                if(isExerciseMatch(exerciseInfo,courseid,kpointid)){
                    exerciseInfos.add(exerciseInfo);
                }
            }
        }

        if(completions != null) {
            for(i=0;i<completions.size();i++){
                exerciseInfo = exerciseInfoMapper.selectByPrimaryKey(completions.get(i).getExerinfoid());
                if(isExerciseMatch(exerciseInfo,courseid,kpointid)){
                    exerciseInfos.add(exerciseInfo);
                }
            }
        }

        if(shortAnswers != null){
            for(i=0;i<shortAnswers.size();i++){
                exerciseInfo = exerciseInfoMapper.selectByPrimaryKey(shortAnswers.get(i).getExerinfoid());
                if(isExerciseMatch(exerciseInfo,courseid,kpointid)){
                    exerciseInfos.add(exerciseInfo);
                }
            }
        }

        return getExercisesByInfos(exerciseInfos);
    }
}
